from rest_framework import serializers
from rest_framework.permissions import IsAdminUser
from rest_framework.viewsets import ModelViewSet

from goods.models import SPU, SPUSpecification, SpecificationOption, GoodsCategory, Brand, SKUImage


class GoodsSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SPU
        fields = ('id', 'name')


class SpecificationOptionSerializer(serializers.ModelSerializer):
    class Meta:
        model = SpecificationOption
        fields = ('id', 'value')


class GoodsSpecsSerializer(serializers.ModelSerializer):
    options = SpecificationOptionSerializer(label='选项', many=True)

    class Meta:
        model = SPUSpecification
        fields = ('id', 'name', 'options')


class GoodsSerializer(serializers.ModelSerializer):
    brand = serializers.StringRelatedField(label='品牌', read_only=True)
    # image = serializers.ImageField(label='spu图片', read_only=True)

    class Meta:
        model = SPU
        fields = ('id', 'name', 'brand', 'brand_id', 'category1_id', 'category2_id',
                  'category3_id', 'sales', 'comments', 'desc_detail',
                  'desc_pack', 'desc_service')
        extra_kwargs = {
            'sales': {'read_only': True},
            'comments': {'read_only': True}
        }

    def create(self, validated_data):
        print(validated_data)
        image = validated_data.get('desc_detail')
        if image:
            validated_data.pop('desc_detail')
        data = super().create(validated_data)
        return data


class GoodsChannelCategoriesSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ('id', 'name')


class GoodsBrandsSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Brand
        fields = ('id', 'name')


class GoodsChannelCategoriessSerializer(serializers.ModelSerializer):
    subs = GoodsChannelCategoriesSerializer(many=True)

    class Meta:
        model = GoodsCategory
        fields = ('id', 'name', 'subs')


class GoodsSpecssSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField(label='spu')
    spu_id = serializers.IntegerField(label='spu_id')

    class Meta:
        model = SPUSpecification
        fields = ('id', 'name', 'spu', 'spu_id')
